<?php

class Pdocombat extends PdoConnect {

    public function create(combat $combat) {
        try {
            $this->open();
            $sql = "INSERT INTO `combat`(IDCOMBAT, IDCATEGORIE, IDREUNION, TITRECOMBAT) VALUES(?, ?, ?, ?)";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $combat->getIDCOMBAT(), PDO::PARAM_INT);
            $prep->bindValue(2, $combat->getIDCATEGORIE(), PDO::PARAM_INT);
            $prep->bindValue(3, $combat->getIDREUNION(), PDO::PARAM_INT);
            $prep->bindValue(4, $combat->getTITRECOMBAT(), PDO::PARAM_STR);
            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readAll() {
        try {
            $this->open();
            $sql = "SELECT * From `combat` ORDER BY TITRECOMBAT";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();
            $prep->setFetchMode(PDO::FETCH_ASSOC);


            return $prep;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readOne($IdCombat) {
        try {
            $this->open();
            $sql = "SELECT * FROM `combat` WHERE IDCOMBAT=$IdCombat";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();

            $combat = new Combat();
            $ligne = $prep->fetch(PDO::FETCH_OBJ);
            $combat->setIdCombat($ligne->IDCOMBAT);
            $combat->setIdCategorie($ligne->IDCATEGORIE);
            $combat->setIdReunion($ligne->IDREUNION);
            $combat->setTitreCombat($ligne->TITRECOMBAT);


            return $combat;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function delete(combat $combat) {

        try {
            $this->open();
            $sql = "DELETE FROM combat WHERE IDCOMBAT=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $combat->getIdCombat(), PDO::PARAM_INT);
            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function update(combat $combat) {
        try {
            $this->open();
            $sql = "UPDATE combat SET TITRECOMBAT = ?, IDCATEGORIE=?, IDREUNION=? WHERE IDCOMBAT=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $combat->getTitreCombat(), PDO::PARAM_STR);
            $prep->bindValue(2, $combat->getIdCategorie(), PDO::PARAM_INT);
            $prep->bindValue(3, $combat->getIdReunion(), PDO::PARAM_INT);
            $prep->bindValue(4, $combat->getIdCombat(), PDO::PARAM_INT);


            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function exist($IdCombat) {
        $this->open();
        $query = "SELECT * FROM combat WHERE IDCOMBAT='$IdCombat';";
        $statement = $this->pdo->query($query);
        return $statement->rowCount();
    }

}
